package com.qf.servlet;

import com.qf.Utils.DaoUtils;
import com.qf.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/addstudent")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name  =req.getParameter("studentName");
        String age   =req.getParameter("studentAge");
        String sex   =req.getParameter("sex");
        String scores   =req.getParameter("studentStore");
        if (name == null && age == null && sex == null && scores == null) {
            return;
        }
        System.out.println(name+" "+age+" "+sex+" "+scores);

        if (!checkAdd(name,age,sex,scores)) {
            resp.sendRedirect("addstudent.jsp?status=2");
            return;
        }

        Double score = new Double(req.getParameter("studentStore"));
        Student student = new Student(name,age,sex,score);

        //添加学生
        DaoUtils.addStudents(student);
        resp.sendRedirect("addstudent.jsp?status=1");

    }
    //判定是否没全部填写
    private boolean checkAdd(String name, String age, String sex, String scores) {

        if (name==null ||name.contains(" ") || "".equals(name)){
            return  false;
        }
        if (age==null ||age.contains(" ") || "".equals(age)){
            return  false;
        }
        if (sex==null ||sex.contains(" ") || "".equals(sex)){
            return  false;
        }
        if (scores==null ||scores.contains(" ") || "".equals(scores)){
            return  false;
        }

        return true;
    }


}
